package com.openfarmanager.android.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.openfarmanager.android.App;
import com.openfarmanager.android.R;

import static com.openfarmanager.android.controllers.EditViewController.*;

/**
 * Toolbar for file viewer
 */
public class ViewerToolbar extends Fragment {

    private Handler mHandler;
    private View mRootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.viewer_toolbar, container, false);
        return mRootView;
    }

    @Override
    public void onStart() {
        super.onStart();

        addClickHandler(R.id.search, SEARCH);
        addClickHandler(R.id.replace, REPLACE);
        addClickHandler(R.id.save, SAVE);
        addClickHandler(R.id.edit, EDIT);
        addClickHandler(R.id.go_to, GOTO);

    }

    public void addClickHandler(final int id, final int msg) {
        mRootView.findViewById(id).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendMessage(msg);
            }
        });
    }

    public void setTextChanged(boolean isTextChanged) {
        String save = App.sInstance.getString(R.string.btn_save);
        ((TextView) getView().findViewById(R.id.save)).setText((isTextChanged ? "* " : "") + save);
    }

    public void setHandler(Handler handler) {
        mHandler = handler;
    }

    private void sendMessage(int what) {
        if (mHandler != null) {
            mHandler.sendEmptyMessage(what);
        }
    }

    public void changeEditText(final String text) {
        ((TextView) mRootView.findViewById(R.id.edit)).setText(text);
    }

    public void setBigFileMode(boolean visible) {
        mRootView.findViewById(R.id.edit).setVisibility(visible ? View.VISIBLE : View.GONE);
        mRootView.findViewById(R.id.save).setVisibility(visible ? View.VISIBLE : View.GONE);
        mRootView.findViewById(R.id.replace).setVisibility(visible ? View.VISIBLE : View.GONE);
    }

}
